﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Fashion_API;

namespace Fashion.BackEnd.ManageAccount
{
    public partial class ManageUserRole : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        private void BindData()
        {
            using (DALDataContext d = new DALDataContext())
            {
                grdUserRole.DataSource = d.UserRoles;
                grdUserRole.DataBind();
            }
        }

        protected void grdUserRole_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "cmdAddNew":
                    using (DALDataContext d = new DALDataContext())
                    {
                        UserRole userRole = new UserRole();
                        TextBox txtUserRole = (TextBox) e.Item.FindControl("txtUserRole");
                        TextBox txtLevelRole = (TextBox)e.Item.FindControl("txtLevelRole");
                        userRole.Name = txtUserRole.Text;
                        userRole.LevelRole = Convert.ToInt32(txtLevelRole.Text);
                        userRole.IsActive = true;
                        d.UserRoles.InsertOnSubmit(userRole);
                        d.SubmitChanges();
                    }
                    BindData();
                    break;
                case "cmdUpdate":
                    using (DALDataContext d = new DALDataContext())
                    {
                        UserRole userRole = d.UserRoles.FirstOrDefault(i => i.Id == Convert.ToInt32(e.CommandArgument));
                        TextBox txtUserRole = (TextBox) e.Item.FindControl("txtUserRole");
                        TextBox txtLevelRole = (TextBox)e.Item.FindControl("txtLevelRole");
                        userRole.Name = txtUserRole.Text;
                        userRole.LevelRole = Convert.ToInt32(txtLevelRole.Text);
                        userRole.IsActive = true;
                        d.SubmitChanges();
                    }
                    BindData();
                    break;
                case "cmdDelete":
                    using (DALDataContext d= new DALDataContext())
                    {
                        UserRole userRole = d.UserRoles.FirstOrDefault(i => i.Id == Convert.ToInt32(e.CommandArgument));
                        d.UserRoles.DeleteOnSubmit(userRole);
                        d.SubmitChanges();
                    }
                    BindData();
                    break;
            }
        }
    }
}